16. Optional Type
Optional Type
In this section, you will learn to:
- Use the
java.util.Optional
class - Use
Optional
with the Stream API
ND079 JPND C2 L01 A13 Optional Type V2
What is an Optional Type?
java.util.Optional
is a container object that may or may not contain a single, non-null value.- Optional is an alternative to using
null
to represent the absence of a value.
Optional Type Example
Optional Types are often returned by terminal operations on streams.
int getTopScore(List<Student> students) {
return students.stream()
.filter(Objects::nonNull)
.mapToInt(Student::getScore)
.max()
.orElse(0);
}
Here, the max()
method actually returns an OptionalInt
, not an int
. If the students
list is empty, the max()
method will return an empty optional.
If max()
returns an OptionalInt
with a value, that value will be used. However, if max()
returns OptionalInt.empty()
, the call to orElse()
makes sure that a default value of 0
will be returned.
This example also shows you how, in addition to Optional<T>
, Java also has optional types that are specialized for int
, long
, and double
primitives. These classes avoid the need for auto-boxing and auto-unboxing of their values.
When to Use Optional Types
When you're designing Java APIs, you should consider using Optional
instead of null
to represent the absence of values.
Optional
can have methods invoked on it without throwing NullPointerException
. The Stream API uses optional types for many of its terminal operations.
However, optionals can sometimes lead to more verbose code by forcing you to call .get()
whenever you want the value.
SOLUTION:
- Methods like `equals` and `toString` are called on an object instead of a `null` reference.
- Signals that the API caller should check if there is a value before using it.
- Methods like `map` and `orElse` can be used on both empty and non-empty `Optional`s.